﻿<!DOCTYPE html>
<html lang="en">
<head>
    <title id="Description">Integration of jqxDateTimeInput with Knockout</title>
    <link rel="stylesheet" href="../../jqwidgets/styles/jqx.base.css" type="text/css" />
    <script type="text/javascript" src="../../scripts/jquery-1.8.1.min.js"></script>
    <script type="text/javascript" src="../../scripts/knockout-2.1.0.js"></script>
    <script type="text/javascript" src="../../jqwidgets/jqxcore.js"></script>
    <script type="text/javascript" src="../../jqwidgets/jqxbuttons.js"></script>
    <script type="text/javascript" src="../../scripts/gettheme.js"></script>
    <script type="text/javascript" src="../../jqwidgets/jqxdatetimeinput.js"></script>
    <script type="text/javascript" src="../../jqwidgets/jqxcalendar.js"></script>
    <script type="text/javascript" src="../../jqwidgets/globalization/jquery.global.js"></script>
    <script type="text/javascript">
        (function ($, ko) {
            ko.jqwidgets = ko.jqwidgets || {};

            ko.jqwidgets.dataBinding = function (settings) {
                var me = this;
                var binding = {},
                name = settings.name;
                var updating = false;
                var updatingFromObservable = false;

                binding.init = function (element, valueAccessor, allBindingAccessor, viewModel) {
                    var unwrappedValue = ko.utils.unwrapObservable(valueAccessor());
                    var modelOptions = ko.toJS(unwrappedValue);
                    widget = $.data(element)[name].instance;

                    if (settings.events) {
                        $.each(settings.events, function () {
                            var eventName = this;
                            $(element).bind(eventName + '.' + element.id, function (event) {
                                if (!updatingFromObservable) {
                                    updating = true;
                                    var val = valueAccessor();
                                    var property = settings.getProperty(widget, event, eventName);
                                    if (val.value && $.isFunction(val.value)) {
                                        val.value(property.value);
                                    }
                                    else if (val.value) {
                                        valueAccessor(property.value);
                                    }

                                    updating = false;
                                }
                            });
                        });
                    }
                };

                binding.update = function (element, valueAccessor, allBindingAccessor, viewModel) {
                    var unwrappedValue = ko.utils.unwrapObservable(valueAccessor());
                    var modelOptions = ko.toJS(unwrappedValue);
                    widget = $.data(element)[name].instance;
                    if (updating)
                        return;

                    $.each(settings, function (name, value) {
                        if (widget[name] && modelOptions[name]) {
                            if (!updating) {
                                updatingFromObservable = true;
                                settings.setProperty(widget, name, widget[name], modelOptions[name]);
                                updatingFromObservable = false;
                            }
                        }
                    });
                };

                ko.bindingHandlers[settings.name] = binding;
            };

            ko.jqwidgets.dataBinding = new ko.jqwidgets.dataBinding({
                name: "jqxDateTimeInput",
                value: null,
                events: ['valuechanged'],
                getProperty: function (object, event, eventName) {
                    if (eventName == 'valuechanged') {
                        return { name: "value", value: event.args.date };
                    }
                },
                setProperty: function (object, key, value, newValue) {
                    if (key == 'value') {
                        object.setDate(newValue);
                    }
                }
            });

        } (jQuery, ko));

        $(document).ready(function () {
            var theme = getTheme();
            $("#jqxdatetimeinput").jqxDateTimeInput({ width: '250px', height: '25px', theme: theme });
            $("#button").jqxButton({ width: 100, height: 25, theme: theme });

            var Model = function (date) {
                this.date = ko.observable(date);
            };

            var date = new Date();
            date.setFullYear(2012, 5, 5);
            var model = new Model(date);
            ko.applyBindings(model);

            $("#button").click(function () {
                alert("Date: " + model.date());
            });
        });
    </script>
</head>
<body class='default'>
    <div data-bind="jqxDateTimeInput: {value: date}" id="jqxdatetimeinput">
    </div>
    <div style="margin-top: 5px; clear: both;">
        <input id="button" type="button" value="Get Value" />
   </div>
</body>
</html>
